package com.buddy.sds.auth.controller;

import com.buddy.sds.auth.entity.PermissionTree;
import com.buddy.sds.auth.entity.SystemPermission;
import com.buddy.sds.auth.entity.SystemUser;
import com.buddy.sds.auth.jwt.JwtTokenManager;
import com.buddy.sds.auth.service.ISystemPermissionService;
import com.buddy.sds.auth.service.ISystemRoleService;
import com.buddy.sds.auth.service.ISystemUserService;
import com.buddy.sds.auth.utils.JWTUtil;
import com.buddy.sds.auth.utils.PasswordUtil;
import com.buddy.sds.common.RestApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/auth")
public class AuthController {

    @Autowired
    private ISystemPermissionService permissionService;

    @Autowired
    private ISystemUserService userService;

    @Autowired
    private ISystemRoleService roleService;

    @Autowired
    private JwtTokenManager tokenManager;

    @PostMapping("/login")
    public RestApiResponse login(SystemUser user) {

        String token =tokenManager.generateAndSave(user);

        RestApiResponse restApiResponse = new RestApiResponse(0, "登录成功", token);

        return restApiResponse;

    }

    /**
     * 获取认证信息
     * @return
     */
    @GetMapping("/getAuthInfo")
    public RestApiResponse getAuthInfo(SystemUser systemUser){


        List<String> roleNames = roleService.getRoleNameList(systemUser.getUserId());

        Set<String> permissionKeys = permissionService.getPermissionKeyList(systemUser.getUserId());

        Map<String,Object> resultMap = new HashMap<>();

        //清除密码
        systemUser.setUserPassword(null);

        resultMap.put("roles",roleNames);
        resultMap.put("permissions",permissionKeys);
        resultMap.put("user",systemUser);

        return new RestApiResponse(0,"",resultMap);
    }

    /**
     * 获取当前登录用户菜单及action树
     *
     * @return
     */
    @GetMapping("/getMenus")
    public RestApiResponse getMenus(SystemUser user) {
        List<PermissionTree> permissionTreeList = permissionService.getPermissionTreeByUser(user.getUserId(), false);
        return new RestApiResponse(0, "", permissionTreeList);
    }

    /**
     * 获取首页path
     * @return
     */
    @GetMapping("/getHomePagePath")
    public RestApiResponse getHomePage(){

        String homePage = permissionService.getHomePagePath();

        return new RestApiResponse(homePage != null ? 0 : -1 ,"",homePage);
    }

    /**
     * 更新密码
     * @param oldPassword
     * @param newPassword
     * @return
     */
    @PostMapping("/updatePassword")
    public RestApiResponse updatePassword(SystemUser systemUser, @RequestParam("oldPassword") String oldPassword,@RequestParam("newPassword") String newPassword){

        int status = -1;

        String message = null;

        Object data = null;

        do {
            if (!PasswordUtil.equals(systemUser.getUserPassword(), oldPassword)) {

                message = "旧密码错误";
                break;
            }

            if(PasswordUtil.equals(systemUser.getUserPassword(), newPassword)){

                message = "旧密码与新密码相同";
                break;
            }
            if(newPassword.length() < 6 || newPassword.length() > 20){
                message = "密码长度应该在6-20个字符长度";
                break;
            }
            systemUser.setUserPassword(PasswordUtil.encode(newPassword));

            userService.updateUserPassword(systemUser.getUserId(),newPassword);

            status  = 0;
            message = "更新成功";

            //更新token
            String token = tokenManager.generateAndSave(systemUser);
            data = token;


        }while (false);

        return new RestApiResponse(status,message,data);
    }

}
